<?php
namespace Home\Controller;
use Think\Controller;
use Think\Log;//日志
class IndexController extends Controller {
    public function index(){
		/*$house=M('house');
		$houselist=$house->select();
		$this->assign('houselist',$houselist);
        $this->display('Setting:list');*/
		$this->display('Index:index');
		//$this->display('App:index');
    }
	
	public function home(){
		$house=M('house');
		$houselist=$house->select();
		$this->assign('houselist',$houselist);
		$intval=C('DASHBOARD_REFRESH');
		$this->assign('intval',$intval);
        $this->display('Index:home');
	}
	
	public function map(){
		$intval=C('DASHBOARD_REFRESH');
		$this->assign('intval',$intval);
        $this->display('Index:map');
	}
	public function loadHouse820410Status()
	{
		$house=M('house');
		$vnm3000status=M('vnm3000status');
		$vbw800status=M('vbw800status');
		$vnm820status=M('vnm820status');
		$vnm410status=M('vnm410status');
		
		$houseid = I("rid");
		$housemodel=$house->where(" rid=".$houseid)->find();
		$nm820410status=$vnm820status->where(" houseid=".$houseid)->find();
		if(is_null($nm820410status)){
			$nm820410status=$vnm410status->where(" houseid=".$houseid)->find();
			if(is_null($nm820410status)){
				$nm820410status['type']='离线';
				$nm820410status['online']='0';
			}else{
				$nm820410status['type']='NM410';
				$nm820410status['online']='1';
				$temp_r_array=explode(',',$nm820410status['temp_r']);
				for($i=0;$i<count($temp_r_array);$i++){
					$nm820410status['temp_r'.($i+1)]=$temp_r_array[$i];
				}
				$humi_r_array=explode(',',$nm820410status['humi_r']);
				for($i=0;$i<count($humi_r_array);$i++){
					$nm820410status['humi_r'.($i+1)]=$humi_r_array[$i];
				}
				$relaystate_dec=$nm820410status['relaystate'];
				$bin=(string)decbin((int)$relaystate_dec);
				$bin=str_pad($bin,16,"0",STR_PAD_LEFT); //补0
				for($j=0;$j<8;$j++){
					$nm820410status['fan'.($j+1)]=substr(strrev($bin),$j,1);
				}
				$nm820410status['fancount']=8;
			}
		}else{
			$nm820410status['type']='NM820';
			$nm820410status['online']='1';
			$temp_r_array=explode(',',$nm820410status['temp_r']);
			for($i=0;$i<count($temp_r_array);$i++){
				$nm820410status['temp_r'.($i+1)]=$temp_r_array[$i];
			}
			$humi_r_array=explode(',',$nm820410status['humi_r']);
			for($i=0;$i<count($humi_r_array);$i++){
				$nm820410status['humi_r'.($i+1)]=$humi_r_array[$i];
			}
			$relaystate_dec=$nm820410status['relaystate'];
			$bin=(string)decbin((int)$relaystate_dec);
			$bin=str_pad($bin,32,"0",STR_PAD_LEFT); //补0
			for($j=0;$j<16;$j++){
				$nm820410status['fan'.($j+1)]=substr(strrev($bin),$j,1);
			}
			$nm820410status['fancount']=16;
		}
		
		echo json_encode($nm820410status);
	}
	
	public function house(){
		$house=M('house');
		
		$vnm3000status=M('vnm3000status');
		$vbw800status=M('vbw800status');
		$vnm820status=M('vnm820status');
		$vnm410status=M('vnm410status');
		
		$houseid = I("rid");
		$houselist=$house->select();
		$housemodel=$house->where(" rid=".$houseid)->find();
		
		$vnm3000statusModel=$vnm3000status->where(" houseid=".$houseid)->find();
		$vbw800statusList=$vbw800status->where(" houseid=".$houseid)->select();
		
		//$nm820410status=array();
		$nm820410status=$vnm820status->where(" houseid=".$houseid)->find();
		if(is_null($nm820410status)){
			$nm820410status=$vnm410status->where(" houseid=".$houseid)->find();
			if(is_null($nm820410status)){
				$nm820410status['device']='离线';
				$nm820410status['online']='0';
			}else{
				$nm820410status['device']='NM410';
				$nm820410status['online']='1';
				$temp_r_array=explode(',',$nm820410status['temp_r']);
				for($i=0;$i<count($temp_r_array);$i++){
					$nm820410status['temp_r'.($i+1)]=$temp_r_array[$i];
				}
				$humi_r_array=explode(',',$nm820410status['humi_r']);
				for($i=0;$i<count($humi_r_array);$i++){
					$nm820410status['humi_r'.($i+1)]=$humi_r_array[$i];
				}
				$relaystate_dec=$nm820410status['relaystate'];
				$bin=(string)decbin((int)$relaystate_dec);
				$bin=str_pad($bin,16,"0",STR_PAD_LEFT); //补0
				for($j=1;$j<9;$j++){
					$nm820410status['fan'.($j)]=substr(strrev($bin),$j,1);
				}
				$nm820410status['fancount']=8;
			}
		}else{
			$nm820410status['device']='NM820';
			$nm820410status['online']='1';
			$temp_r_array=explode(',',$nm820410status['temp_r']);
			for($i=0;$i<count($temp_r_array);$i++){
				$nm820410status['temp_r'.($i+1)]=$temp_r_array[$i];
			}
			$humi_r_array=explode(',',$nm820410status['humi_r']);
			for($i=0;$i<count($humi_r_array);$i++){
				$nm820410status['humi_r'.($i+1)]=$humi_r_array[$i];
			}
			$relaystate_dec=$nm820410status['relaystate'];
			$bin=(string)decbin((int)$relaystate_dec);
			$bin=str_pad($bin,32,"0",STR_PAD_LEFT); //补0
			for($j=1;$j<17;$j++){
				$nm820410status['fan'.($j)]=substr(strrev($bin),$j,1);
			}
			$nm820410status['fancount']=16;
		}
		
		
		$this->assign('housemodel',$housemodel);
		$this->assign('vnm3000statusModel',$vnm3000statusModel);
		$this->assign('nm820410status',$nm820410status);
		$this->assign('vbw800statusList',$vbw800statusList);
		$this->assign('houselist',$houselist);
		$this->assign('houseid',$houseid);
		$intval=C('DASHBOARD_REFRESH');
		$this->assign('intval',$intval);
        $this->display('Index:house');
	}

	
	public function loadbw800(){
		$houseid = I("rid");
		$vbw800status=M('vbw800status');
		$list=array();
		if(empty($houseid)){
			$vbw800statusList=$vbw800status->select();
		}else{
			$vbw800statusList=$vbw800status->where("houseid=".$houseid)->select();
		}
		echo json_encode($vbw800statusList);
	}

	public function loadnm3000(){
		$houseid = I("rid");
		$vnm3000status=M('vnm3000status');
		$list=array();
		if(empty($houseid)){
			$vnm3000statusList=$vnm3000status->select();
		}else{
			$vnm3000statusList=$vnm3000status->where("houseid=".$houseid)->select();
		}
		
		echo json_encode($vnm3000statusList);
	}
	
	public function loadnm820(){
		$houseid = I("rid");
		$vnm820status=M('vnm820status');
		$list=array();
		if(empty($houseid)){
			$vnm820statusList=$vnm820status->select();
		}else{
			$vnm820statusList=$vnm820status->where("houseid=".$houseid)->select();
		}
		echo json_encode($vnm820statusList);
	}

	public function loadnm410(){
		$houseid = I("rid");
		$vnm410status=M('vnm410status');
		$list=array();
		if(empty($houseid)){
			$vnm410statusList=$vnm410status->select();
		}else{
			$vnm410statusList=$vnm410status->where("houseid=".$houseid)->select();
		}
		
		echo json_encode($vnm410statusList);
	}
	
	public function loadnm820410(){
		$houseid = I("rid");
		$vnm820status=M('vnm820status');
		$vnm410status=M('vnm410status');
		$list=array();
		if(empty($houseid)){
			$list1=$vnm820status->select();
			$list2=$vnm410status->select();
		}else{
			$list1=$vnm820status->where("houseid=".$houseid)->select();
			$list2=$vnm410status->where("houseid=".$houseid)->select();
		}
		
		$list['list410']=$list2;
		$list['list820']=$list1;
		echo json_encode($list);
	}
}